/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sunrise.bean;

import com.sunrise.boundary.CalendarManager;
import com.sunrise.boundary.UserManager;
import com.sunrise.entity.Calendar;
import com.sunrise.entity.User;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;

/**
 *
 * @author Andrea
 */
@Named(value = "settingsBean")
@RequestScoped
public class SettingsBean {
    
    @EJB
    private UserManager userManager;
    
    @EJB
    private CalendarManager calendarManager;
    
    private User user;
    
    private Calendar calendar;
    
    public SettingsBean() {
    }
    
    @PostConstruct
    public void init() {
        user = userManager.getLoggedUser();
        List<com.sunrise.entity.Calendar> calendars = user.getCalendars();

        if (calendars.size() > 0) {
                calendar = calendars.get(0);
        } else {
            calendar = new Calendar("main");
            user.addCalendar(calendar);
        }
    }
    
    public User getUser() {
        return user;
    }
    
    public void setUser(User user) {
        this.user = user;
    }

    public Calendar getCalendar() {
        return calendar;
    }

    public void setCalendar(Calendar calendar) {
        this.calendar = calendar;
    }
 
    public String save() {
        
        userManager.update(user);
        return "calendar?faces-redirect=true";
    }
}
